package com.xmlt.core.template.instance;

import java.util.List;
import java.util.Map;

import com.xmlt.core.template.TemplateException;

public class AbstrcatInstanceVisitor implements IInstanceVisitor {

	@Override
	public void visit(AbstractInstance abstractInstance) throws TemplateException {
		if (abstractInstance instanceof Instance) {
			visit((Instance) abstractInstance);
		} else if (abstractInstance instanceof MultiInstance) {
			visit((MultiInstance) abstractInstance);
		} else if (abstractInstance instanceof InstanceReference) {
			visit((InstanceReference) abstractInstance);
		} else if (abstractInstance instanceof Value) {
			visit((Value) abstractInstance);
		}
	}

	@Override
	public void visit(Instance instance) throws TemplateException {
		Map<String, AbstractInstance> childrenMap = instance.getChildren();
		for (String key: childrenMap.keySet()) {
			visit(childrenMap.get(key));
		}
	}

	@Override
	public void visit(MultiInstance multiInstance) throws TemplateException {
		List<AbstractInstance> children = multiInstance.getChildren();
		for (AbstractInstance child: children) {
			visit(child);
		}
	}

	@Override
	public void visit(Value value) {
	}

	@Override
	public void visit(InstanceReference instanceReference) throws TemplateException {
		visit(instanceReference.resolveReference());
	}

}
